package com.magnificent.atlantis.client.ui.planner.panels.task.treetable;

import com.magnificent.atlantis.client.data.task.planner.AbstractPlannerTask;
import com.magnificent.atlantis.client.ui.planner.panels.task.treetable.data.PlannerTableNode;
import com.magnificent.atlantis.client.ui.planner.panels.task.treetable.data.PlannerTableShell;
import com.magnificent.atlantis.client.ui.planner.panels.task.treetable.renderers.PlannerTableCellRenderer;
import com.magnificent.atlantis.client.ui.planner.panels.task.treetable.renderers.PlannerTreeCellRenderer;
import com.magnificent.helpers.swing.TableHelper;
import org.jdesktop.swingx.JXTreeTable;
import org.jdesktop.swingx.treetable.TreeTableModel;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * User: Babanin
 * Date: 05.03.2009
 * Time: 23:11:27
 */
public class PlannerTreeTable extends JXTreeTable {

    private List<PlannerTableNode> expandedNodes;

    public PlannerTreeTable(TreeTableModel treeModel) {
        setTreeTableModel(treeModel);
        setTreeCellRenderer(new PlannerTreeCellRenderer());
        setDefaultRenderer(Object.class, new PlannerTableCellRenderer());

        expandedNodes = new ArrayList<PlannerTableNode>();
    }

    public void select(int row) {
        if (row >= 0) {
            getSelectionModel().setSelectionInterval(row, row);
        }
    }

    public void select(PlannerTableNode tableNode) {
        select(find(tableNode));
    }

    public int select(AbstractPlannerTask abstractPlannerTask) {
        int row = find(abstractPlannerTask);

        select(row);

        return row;
    }

    private int find(AbstractPlannerTask abstractPlannerTask) {
        int result = -1;
        for (int i = 0; i < getRowCount(); i++) {
            if (((PlannerTableShell) getValueAt(i, 0)).getNode().getTask() == abstractPlannerTask) {
                result = i;
                break;
            }
        }

        return result;
    }

    public int find(PlannerTableNode tableNode) {
        int result = -1;
        for (int i = 0; i < getRowCount(); i++) {
            if (((PlannerTableShell) getValueAt(i, 0)).getNode().equals(tableNode)) {
                result = i;
                break;
            }
        }

        return result;
    }

    public void saveExpandedNodes() {
        PlannerTableNode root = (PlannerTableNode) getTreeTableModel().getRoot();

        Queue<PlannerTableNode> nodes = new LinkedList<PlannerTableNode>();
        nodes.add(root);

        while (nodes.size() != 0) {
            PlannerTableNode tableNode = nodes.poll();

            if (isExpanded(tableNode.getTreePath())) {
                expandedNodes.add(tableNode);
                System.out.println(tableNode);
            }

            for (PlannerTableNode node : tableNode.getChildren()) {
                nodes.add(node);
            }
        }
    }

    public void restoreExpandedNodes() {
        PlannerTableNode root = (PlannerTableNode) getTreeTableModel().getRoot();

        Queue<PlannerTableNode> nodes = new LinkedList<PlannerTableNode>();
        nodes.add(root);

        while (nodes.size() != 0) {
            PlannerTableNode tableNode = nodes.poll();

            for (PlannerTableNode expandedNode : expandedNodes) {
                if (expandedNode.getTask() == tableNode.getTask()) {
                    expandPath(tableNode.getTreePath());
                }
            }

            for (PlannerTableNode node : tableNode.getChildren()) {
                nodes.add(node);
            }
        }
    }

    public void setTreeTableModel(org.jdesktop.swingx.treetable.TreeTableModel treeModel) {
        super.setTreeTableModel(treeModel);

        TableHelper.setFixedColumnSize(this, 0, 40);
        TableHelper.setFixedColumnSize(this, 1, 40);
        TableHelper.setFixedColumnSize(this, 3, 90);
        TableHelper.setFixedColumnSize(this, 4, 130);
    }
}
